/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.EventDaoImpl;
import dao.UserDao;
import entities1.Event;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author takamflore
 */
public class UpdateEventServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @EJB
    EventDaoImpl eventDao;
    @EJB
    UserDao user;
    String forward="";
    
    public UpdateEventServlet() {
        super();
        eventDao = new EventDaoImpl();
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String Events;
        Event event=new Event();
          int i= user.getUserId(request.getRemoteUser());    
         String action = request.getParameter("action");

        if (action.equalsIgnoreCase("Update")){
            
            event.setId(Integer.parseInt(request.getParameter("eventId")));
            event.setNameEvent(request.getParameter("nameEvent"));
            event.setOrganizer( user.getUser(i));
            //date event
          try {
            Date date = new SimpleDateFormat("MM/dd/yyyy").parse(request.getParameter("date"));
            event.setDateEvent(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
            //hour event
          try {
            Date hour = new SimpleDateFormat("HH:mm:ss").parse(request.getParameter("hour"));
            event.setHour(hour);
        } catch (ParseException e) {
            e.printStackTrace();
        }
            
            event.setAddress(request.getParameter("address"));
            event.setDescription(request.getParameter("description"));
            
            event.setWeather(null);
            event.setInvitedCollection(null);
            
            eventDao.updateEvent(event);          
            request.setAttribute("Events", eventDao.getAllEvent(i));
            
        }else{
            request.setAttribute("Events", eventDao.getAllEvent(i));
        }
        forward ="WEB-INF/entity/event/List_Event.jsp";
         RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
        
      
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         int i= user.getUserId(request.getRemoteUser());
         request.setAttribute("Events", eventDao.getAllEvent(i));
         forward ="WEB-INF/entity/event/List_Event.jsp";
         RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
       
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
